<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./style.css">

</head>

<body>
    <div id="app"></div>

    <script src="../react/react.development.js"></script>
    <script src="../react/react-dom.development.js"></script>
    <script src="../react/babel.min.js"></script>

    <script type="text/babel">
        class TabControl extends React.Component {
            constructor(props) {
                super(props);

                this.state = {
                    currentIndex: 0
                }
            }

            render() {
                const { titles } = this.props;
                const { currentIndex } = this.state;

                return (
                    <div className="tab-control">
                        {
                            titles.map((item, index) => {
                                return (
                                    <div key={item}
                                        className={"tab-item " + (index === currentIndex ? "active" : "")}
                                        onClick={e => this.itemClick(index)}>
                                        <span>{item}</span>
                                    </div>
                                )
                            })
                        }
                    </div>
                )
            }

            itemClick(index) {
                this.setState({
                    currentIndex: index
                })
                //====>>>获取到父组件中传递过来的方法 itemClick
                const { itemClick } = this.props;
                itemClick(index);
            }
        }
        class APP extends React.Component {
            constructor(props) {
                super(props);
                //======>>>>一般的如果数据是不变的，可以保存在this的一个属性中，我们尽量把会改变的数据放在state中保存
                this.titles = ['新款', '精选', '流行'];

                this.state = {
                    currentTitle: "新款",
                    currentIndex: 0
                }
            }

            render() {
                const { currentTitle } = this.state;

                return (
                    <div>
                        <TabControl itemClick={index => this.itemClick(index)} titles={this.titles} />
                        <h2>{currentTitle}</h2>
                    </div>
                )
            }

            itemClick(index) {
                this.setState({
                    currentTitle: this.titles[index]
                })
            }
        }

        ReactDOM.render(<APP />, document.getElementById('app'));

    </script>
</body>

</html>